package com.oymn.myapplication.page;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager2.widget.ViewPager2;

import com.oymn.myapplication.R;
import com.oymn.myapplication.adapter.MyFragmentStateAdapter;
import com.oymn.myapplication.utils.Url;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.w3c.dom.Text;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

public class goods extends AppCompatActivity {

    private ViewPager2 viewPager2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_goods);

        TextView goods_name =findViewById(R.id.goods_name);
        TextView goods_introl = findViewById(R.id.goods_introl);
        TextView goods_price = findViewById(R.id.goods_price);
        Button cart = findViewById(R.id.cart);
        Button buy = findViewById(R.id.buy);

        Intent intent = getIntent();
        String goodsName = intent.getStringExtra("goods_name");
        String goodsPrice = intent.getStringExtra("goods_price");
        String goodsIntrol = intent.getStringExtra("goods_introl");
        Bitmap goodsImg = intent.getParcelableExtra("goods_img");
        goods_name.setText(goodsName);
        goods_price.setText(goodsPrice);
        goods_introl.setText(goodsIntrol);

        String user_phone = intent.getStringExtra("user_phone");

        viewPager2=(ViewPager2) findViewById(R.id.v1);
        viewPager2.setAdapter(new MyFragmentStateAdapter(this,goodsImg));

        buy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent();
                intent.setAction("android.intent.action.buy_goods");
                intent.putExtra("com.example.myapplication.buy_goods.inputPrice",goods_price.getText().toString());
                intent.putExtra("com.example.myapplication.buy_goods.inputName",goods_name.getText().toString());
                intent.putExtra("com.example.myapplication.buy_goods.goods_img",goodsImg);

                startActivity(intent);
            }
        });
        cart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addCart(user_phone,goodsName);
                Toast.makeText(goods.this,"加入购物车成功",Toast.LENGTH_SHORT).show();
            }
        });
    }
    //加入购物车
    public void addCart(String user_phone,String goods_name){
        new Thread(new Runnable() {
            @Override
            public void run() {
                HttpClient httpClient = new DefaultHttpClient();
                HttpPost request = new HttpPost(Url.cart_url);
                request.setHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");

                List<NameValuePair> param = new ArrayList<NameValuePair>();
                param.add(new BasicNameValuePair("action","addCart"));
                param.add(new BasicNameValuePair("user_phone",user_phone));
                param.add(new BasicNameValuePair("goods_name",goods_name));

                try {
                    request.setEntity(new UrlEncodedFormEntity(param, HTTP.UTF_8));
                    HttpResponse response = httpClient.execute(request);
                    HttpEntity entity = response.getEntity();
                    //获取响应结果的信息
                    String result = EntityUtils.toString(entity,"UTF-8");
                    //这里就不处理什么结果了，默认是成功加入购物车了
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

}